﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Rsc.Bss.Tags;
using Aulac.Rsc.Bss;
using Aulac.Global.Common;
using Aulac.Global.Xml;
using Aulac.Srv.Data.XmlQuery;
using System.Xml;
using Aulac.Srv.Db.Common;

namespace Aulac.Srv.Db.Data.Ems.Items.RptLists
{
    partial class BuyCapAss
    {
        /// <summary>
        /// ExecQuery Chi tiet mot phieu mua hang.
        /// </summary>
        private XmlDataDocument ExecQueryGetDetailBuyCapAss(string pVchBuyCapAssPrkID)
        {
            try
            {
                SrvExecXmlQueryData query = new SrvExecXmlQueryData(DbDataProvider);

                query.SetVchHeaderElm(QueryCmdTypes.SelectData);
                string sqlQueryH = string.Format("SELECT {0}", StringHelper.ConvertArrayToString(", ", BuyCapAssInfo.mLstVchBuyHeader.ToArray()));
                sqlQueryH += string.Format(" FROM {0}", BuyCapAssInfo.mTbBuyHeader);
                sqlQueryH += string.Format(" WHERE {0} = @{0}", tagVch.VchBuyHeaderCapAss.VchBuyCapAssPrkID);
                query.ObjQueryVchHeader.SqlQueryString = sqlQueryH;
                query.ObjQueryVchHeader.AddValueParamFilterItem(tagVch.VchBuyHeaderCapAss.VchBuyCapAssPrkID, tagVch.VchBuyHeaderCapAss.VchBuyCapAssPrkID, pVchBuyCapAssPrkID);

                query.SetVchLinesElm(QueryCmdTypes.SelectData);
                //string sqlQueryL = string.Format("SELECT L.{0}, dbo.Dm_MdnItems.ItemGrpID", "*");
                //sqlQueryL += string.Format(" FROM {0} AS L ", BuyCapAssInfo.mTbBuyLine);
                //sqlQueryL += string.Format("LEFT OUTER JOIN dbo.Dm_MdnItems ON L.ItemPrkID = dbo.Dm_MdnItems.ItemPrkID");
                //sqlQueryL += string.Format(" WHERE (L.{0} = @{0})", tagVch.VchBuyLineCapAss.VchBuyCapAssPrkID);
                string sqlQueryL = string.Format("SELECT dbo.Dm_MdnItems.ItemName, dbo.Dm_MdnGrpItems.ItemGrpID, dbo.VchLineBuyCapAss.VchBuyCapAssPrkID, dbo.VchLineBuyCapAss.ItemLine, dbo.VchLineBuyCapAss.ItemPrkID, dbo.VchLineBuyCapAss.NoteLine, dbo.VchLineBuyCapAss.Unit, dbo.VchLineBuyCapAss.Quantity, dbo.VchLineBuyCapAss.UnitPrice, dbo.VchLineBuyCapAss.AmtLineInExc, dbo.VchLineBuyCapAss.VAT, dbo.VchLineBuyCapAss.AmtTaxLineInExc, dbo.VchLineBuyCapAss.Specification FROM dbo.Dm_MdnGrpItems LEFT OUTER JOIN dbo.Dm_MdnItems ON dbo.Dm_MdnGrpItems.ItemGrpID = dbo.Dm_MdnItems.ItemGrpID RIGHT OUTER JOIN dbo.VchLineBuyCapAss ON dbo.Dm_MdnItems.ItemPrkID = dbo.VchLineBuyCapAss.ItemPrkID WHERE (dbo.VchLineBuyCapAss.VchBuyCapAssPrkID = '{0}')", pVchBuyCapAssPrkID);
                query.ObjQueryVchLines.SqlQueryString = sqlQueryL;
                query.ObjQueryVchLines.AddValueParamFilterItem(tagVch.VchBuyLineCapAss.VchBuyCapAssPrkID, tagVch.VchBuyHeaderCapAss.VchBuyCapAssPrkID, pVchBuyCapAssPrkID);
            
                
                return query.ExecureQuery();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// ExecQuery Danh sach cac phieu mua hang.
        /// </summary>
        private XmlDataDocument ExecQueryGetListDetailBuyCapAss(string pVchDateFrom, string pVchDateTo)
        {
            string sqlQuery = string.Format("SELECT h.{0}", StringHelper.ConvertArrayToString(" ,h.", BuyCapAssInfo.mLstVchBuyHeader.ToArray()));
            sqlQuery += string.Format(" FROM {0} AS h ", BuyCapAssInfo.mTbBuyHeader);
            sqlQuery += string.Format(" WHERE (h.{0} BETWEEN ? AND ?)", tagVch.VchBuyHeaderCapAss.VchDate);
            try
            {
                DbDataReader = DbDataProvider.ExecuteReader(mDbCmd, sqlQuery, pVchDateFrom, pVchDateTo);
                XmlDataDocument xmlDoc = CreateXmlDocument("", "");
                XmlElement eVLines = XmlProcess.AddVchLinesElm(ref xmlDoc);
                while (DbDataReader.Read())
                {
                    XmlElement eLine = XmlProcess.AddVchLineItemElm(eVLines);
                    DbDataProvider.GetValuesToXml(DbDataReader, ref eLine, BuyCapAssInfo.mLstVchBuyHeader.ToArray());
                }
                DbDataReader.Close();
                return xmlDoc;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Xoa mot Phieu mua hang.
        /// </summary>
        private XmlDataDocument ExecQueryDelVchBuyCapAss(string sVchBuyCapAssPrkID)
        {
            try
            {
                string sqlQureyH = string.Format("DELETE FROM {0}",  BuyCapAssInfo.mTbBuyHeader);
                sqlQureyH += string.Format("WHERE {0} = ?",tagVch.VchBuyHeaderCapAss.VchBuyCapAssPrkID);
                string sqlQueryL = string.Format("DELETE FROM {0}", BuyCapAssInfo.mTbBuyLine);
                sqlQueryL += string.Format("WHERE {0} = ?", tagVch.VchBuyLineCapAss.VchBuyCapAssPrkID);
                try
                {
                    DbDataProvider.ExecuteReader(mDbCmd, sqlQureyH, sVchBuyCapAssPrkID);
                    XmlDataDocument xmlDoc = CreateXmlDocument("", "");
                    XmlProcess.SetAtrbVchHeader(xmlDoc, tagVch.VchBuyHeaderCapAss.VchBuyCapAssPrkID, sVchBuyCapAssPrkID);
                    DbDataProvider.ExecuteReader(mDbCmd, sqlQueryL, sVchBuyCapAssPrkID);
                    return xmlDoc;
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

       
    }
}
